#!/usr/bin/python
#-*-coding:utf-8-*-
# Last Change:  2012-11-10 23:21:34
import MySQLdb
import time
"""
_DB_DAL
"""
#define global variable
mysql_config  =  {
        "mysql_server":"127.0.0.1", 
        "mysql_user":"root", 
        "mysql_pass":"321", 
        "db":"test"
    }
class _DB_DAL(object):
    def __init__(self):
        self._is_open    = False
        self._last_error = None
    def connect(self):
        while True:
            try:
                self._dbOpen()
                self._is_open = True
                break
            except Exception, _ex:
                self._last_error  =  _ex
                self._is_open = False
                time.sleep(1)
    def close(self):
        if self.isConnected():
            self._dbclose()
    def _dbOpen(self):
        pass
    def _dbClose(self):
        pass
    def isConnected(self):
        return self._is_open

    def dbQuery(self, sql):
        pass
    def dbExec(self, sql):
        pass
    def dbCreate(self, dbName):
        pass

class _MySQL_DAL(_DB_DAL):
    def __init__(self):
        super(_MySQL_DAL, self).__init__()
        self.connect()
    def _dbOpen(self):
        self.connection = MySQLdb.connect(
                mysql_config["mysql_server"],
                mysql_config["mysql_user"],
                mysql_config["mysql_pass"],
                connect_timeout=60,
                compress=True,charset="UTF8")
        self.db = self.connection.select_db(mysql_config["db"])
    def _dbClose(self):
        if self.connection:
            self.connection.close()
        self.db = None
        self.connection  =  None
    def dbQuery(self, sql):
        #获取操作游标
        cursor = self.connection.cursor()
        task = cursor.execute(sql)
        if task:
            return cursor.fetchall()
        return None
    def dbExec(self, sql):
        cursor = self.connection.cursor()
        cursor.execute(sql)
        self.connection.commit()
    def dbCreate(self, dbName):
        curs  =  self.connection.cursor()
        curs.execute("create database %s"%dbName)
        self.connection.commit()


class AdsConDB(object):
    def __init__(self):
        self._mysql_dal = _MySQL_DAL()
        self.init()

    def init(self):
        for item in dir(self._mysql_dal):
            setattr(self, "db_%s"%item, 
                     getattr(self._mysql_dal, item))

"""
调用MySQL示例
db = AdsConDB()
tmp = db.db_dbQuery("select  * from abc")
for item in tmp:
    print item[0]
db.db_dbExec("insert into abc(a) values('ee')")
"""
"""
实现将innerDB的库名修改
Bug1: 如果表里面有触发器的时候就不可以
def renameDBS(oldDB, newDB):
    global mysql_config
    mysql_config["db"]  =  "information_schema"
    db = AdsConDB()
    print mysql_config
    db.db_dbCreate(newDB)
    tmp = db.db_dbQuery("select table_name from TABLES where TABLE_SCHEMA = '%s'"%(oldDB))
    for item in tmp:
        _tableName = item[0]
        sql  =  "rename table %s.%s to %s.%s"%(oldDB, _tableName, newDB,
                _tableName)
        print sql
        db.db_dbExec(sql)
renameDBS("test", "test_newname")
"""


